/*
 * Copyright 2008 Lukas Vlcek
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

package org.radio.lastfm.control;

import javax.swing.JButton;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.radio.lastfm.control.action.Tunein;
import org.radio.lastfm.control.action.Play;
import org.radio.lastfm.control.action.Skip;
import org.radio.lastfm.control.action.Stop;
import org.radio.lastfm.service.PlayerService;

/**
 * This class can manage visual availability of play, skip and stop button.
 * Any Component containing any of play, skip or stop button you can do the following:
 * <code>
 *      PlayerButtonControl control =
 *          new PlayerButtonControl(
 *              this.jbPlay, this.jbSkip, this.jbStop,
 *              null // - no tunein control buttons
 *      );
 * </code>
 * Now, buttons will be enabled or disabled according to the PlayerService state.
 */
public class PlayerButtonControl implements LookupListener {

    private JButton play;
    private JButton skip;
    private JButton stop;
    private JButton tunein;
    
    private Lookup lookup;
    private Lookup.Result<Stop> resultStop;
    private Lookup.Result<Play> resultPlay;
    private Lookup.Result<Skip> resultSkip;
    private Lookup.Result<Tunein> resultTunein;
    
    public PlayerButtonControl(JButton play, JButton skip, JButton stop, JButton tunein) {
        
        this.play = play;
        this.skip = skip;
        this.stop = stop;
        this.tunein = tunein;
        
        lookup = PlayerService.getStaticLookup();
        
        resultStop = lookup.lookup(new Lookup.Template(Stop.class));
        resultStop.addLookupListener(this);
        resultStop.allInstances();
        
        resultPlay = lookup.lookup(new Lookup.Template(Play.class));
        resultPlay.addLookupListener(this);
        resultPlay.allInstances();
        
        resultSkip = lookup.lookup(new Lookup.Template(Skip.class));
        resultSkip.addLookupListener(this);
        resultSkip.allInstances();
        
        resultTunein = lookup.lookup(new Lookup.Template(Tunein.class));
        resultTunein.addLookupListener(this);
        resultTunein.allInstances();
        
        resultChanged(null);
    }
    
    public void resultChanged(LookupEvent arg0) {

        if (resultStop.allInstances().size() > 0) {
            if (stop != null) stop.setEnabled(true);
        } else {
            if (stop != null) stop.setEnabled(false);
        }

        if (resultPlay.allInstances().size() > 0) {
            if (play != null) play.setEnabled(true);
        } else {
            if (play != null) play.setEnabled(false);
        }


        if (resultSkip.allInstances().size() > 0) {
            if (skip != null) skip.setEnabled(true);
        } else {
            if (skip != null) skip.setEnabled(false);
        }
        
        if (resultTunein.allInstances().size() > 0) {
            if (tunein != null) tunein.setEnabled(true);
        } else {
            if (tunein != null) tunein.setEnabled(false);
        }
        
        if (resultStop.allInstances().size() == 0 &&
            resultPlay.allInstances().size() == 0 &&
            resultSkip.allInstances().size() == 0 &&
            resultTunein.allInstances().size() == 0)
        {
            if (skip != null) skip.setEnabled(false);
            if (stop != null) stop.setEnabled(false);
            if (play != null) play.setEnabled(false);
            if (tunein != null) tunein.setEnabled(false);
        }
    }

}
